/**
 * @param {number} n
 * @return {boolean}
 */
var isHappy = function (n) {
    const res = new Set();
    while (1) {
        n = change(n);
        if (res.has(n)) {
            return false
        }
        if (n === 1) {
            return true
        }
        res.add(n);
    }

};

let change = function (n) {
    let changeRes = 0;
    while (n >= 10) {
        changeRes += (n % 10) ** 2;
        n = Math.floor(n / 10);
    }
    changeRes += n ** 2;
    return changeRes;
}